package com.mioto.ld.module.personal.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.mioto.ld.exception.BasicException;
import com.mioto.ld.module.personal.model.UserInfoVO;
import com.mioto.ld.module.personal.model.UserInfoDTO;
import com.mioto.ld.module.personal.model.UserStatus;
import com.mioto.ld.result.SystemTip;
import com.mioto.ld.utils.BaseUtil;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.mioto.ld.module.personal.dao.UserDao;
import com.mioto.ld.module.personal.model.User;
import com.mioto.ld.module.personal.service.IUserService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * @author mioto
 * @date 2024-05-09 09:30:37
 */
@Service("userService")
public class UserServiceImpl implements IUserService{

    @Resource
    private UserDao userDao;

    @Override
    public List<UserInfoVO> findList(User user) {
        return userDao.findList(user,BaseUtil.buildUserFilterCondition());
    }

    @Override
    public UserInfoVO findDetailByUsername(String username) {
        return userDao.findDetailByUsername(username);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(UserInfoDTO user) {
        if(ObjectUtil.isEmpty(findByColumn("username",user.getUsername()))) {
            user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));
            user.setStatus(UserStatus.USER_STATUS_ENABLE);
            user.initCreateData();
            int result = userDao.insertIgnoreNull(user);
            if (result > 0 && ObjectUtil.isNotNull(user.getRoleId())) {
                result = userDao.insertUserRoleRelation(user.getId(), user.getRoleId());
            }
            return result;
        }
        throw new BasicException(SystemTip.USERNAME_EXIST);
    }

    @Override
    public int update(User user) {
        return userDao.update(user);
    }

    @Override
    public int updateIgnoreNull(User user) {
        if (StrUtil.isNotEmpty(user.getPassword())){
            user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));
        }
        return userDao.updateIgnoreNull(user);
    }

    @Override
    public User findByColumn(String column, Object value) {
        return userDao.findByColumn(column,value);
    }

    @Override
    public int deleteByColumn(String column, Object value) {
        return userDao.deleteByColumn(column,value);
    }

    @Override
    public int batchDelete(Integer[] ids) {
        return userDao.batchDelete(ids);
    }

    @Override
    public int updatePwd(int userId, String oldPwd, String newPwd) {
        User user = userDao.findByColumn("id",userId);
        if (ObjectUtil.isNotNull(user)) {
            if (!new BCryptPasswordEncoder().matches(oldPwd, user.getPassword())) {
                throw new BasicException(SystemTip.USER_OLD_PWD_MISMATCH);
            }
            User newUser = new User();
            newUser.setId(userId);
            newUser.setPassword(new BCryptPasswordEncoder().encode(newPwd));
            return userDao.updateIgnoreNull(newUser);
        }
        return 0;
    }
}